'use strict';
const Service = require('egg').Service;
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const { _pwd } = require('../../utils/cryp');

class AdminService extends Service {
  // 获取自己用户信息
  async adminInfo() {
    try {
      console.log('==============>', this.ctx);
      const { Admin } = this.app.model.Tables.Com;
      const result = await Admin.findOne({
        where: {
          id: this.ctx.adminInfo.id,
        },
      });
      if (result) {
        return this.ctx.response.success(result);
      }
      return this.ctx.response.error('找不到该用户信息');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }
  // 获取用户列表信息
  async index() {
    try {
      const { Admin } = this.app.model.Tables.Com;
      const result = await Admin.findAll();
      if (result) {
        return this.ctx.response.success(result);
      }
      return this.ctx.response.error('获得用户列表信息失败');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }
  // 获取单个用户
  async show(data) {
    let where = {};
    if (isNaN(Number(data))) {
      where.name = data;
    } else {
      where.id = data;
    }

    try {
      const { Admin } = this.app.model.Tables.Com;
      const result = await Admin.findOne({
        where,
      });
      if (result) {
        return this.ctx.response.success(result);
      }
      return this.ctx.response.error('找不到该用户信息');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }
  // 更新用户信息
  async update(id, data) {
    try {
      // 判断验证码
      // const verify = this.ctx.session.code;
      // console.log('verify ==>', verify);
      // if (verify && (verify.toLowerCase() !== data.verify.toLowerCase())) {
      //   return this.ctx.response.error('验证码错误');
      // }

      const { Admin } = this.app.model.Tables.Com;
      const resultAdmin = await Admin.findByPk(id);
      if (resultAdmin && resultAdmin.dataValues) {
        const result = await resultAdmin.update({ ...data });
        return this.ctx.response.success(result.dataValues);
      }
      return this.ctx.response.error('找不到该用户信息');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }
  // 用户注册
  async create(dataObj) {
    // 判断验证码
    // const verify = this.ctx.session.code;
    // console.log('verify ==>', verify);
    // if (verify && (verify.toLowerCase() !== dataObj.verify.toLowerCase())) {
    //   return this.ctx.response.error('验证码错误');
    // }
    console.log('this is dataObj=====>', dataObj);
    // console.log('================]CTX', this.ctx);
    try {
      const { Admin } = this.app.model.Tables.Com;
      const Shop = this.app.model.Tables.Shop;
      const Web = this.app.model.Tables.Web;
      // 查找手机号码是否已经注册
      const adminInfo = await Admin.findOne({
        where: {
          phone: dataObj.phone,
        },
      });
      if (adminInfo) {
        return this.ctx.response.error('手机号码已注册');
      }

      const result = await Admin.create(dataObj);
      // 默认给所有项目都初始化项目 (默认存在一个项目)
      const project = await Shop.Project.create({
        name: '蓝景项目',
        user_id: result.dataValues.id,
      });
      // 更新项目id到管理台
      result.update({
        project_id: project.dataValues.id,
      });
      return result
        ? this.ctx.response.success(result, '注册成功')
        : this.ctx.response.error(result, '注册失败');
    } catch (error) {
      console.log('---------------------------');
      console.log(error);
      return this.ctx.response.error(error.message);
    }
  }
  // 删除用户
  async destroy(id) {
    try {
      const { ctx } = this;
      const { Admin } = this.app.model.Tables.Com;
      const result = Admin.destroy({
        where: {
          id,
        },
      });
      return result > 0
        ? ctx.response.success('删除成功')
        : ctx.response.error(result, '删除失败');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }

  // 管理台登录
  async loginAdmin(data) {
    try {
      const { Admin } = this.app.model.Tables.Com;
      const { ctx } = this;
      const loginRes = await Admin.findOne({
        where: {
          phone: data.phone,
          password: data.password,
        },
        //商家登录成功后，密码这种敏感信息不应该暴露出来
        // attributes: { exclude: ['password'] },
      });
      if (loginRes) {
        const time = Date.now();
        const { id } = loginRes.dataValues;
        const { password: token } = new _pwd(id + time);
        // 添加token到商家表，默认过期时间一天
        await loginRes.update({
          token,
          token_out_of_date: new Date().getTime() + 24 * 60 * 60 * 1000,
          last_ip: ctx.request.ip,
        });
        return ctx.response.success(loginRes, '登录成功');
      }
      return ctx.response.error('账号或密码错误');
    } catch (error) {
      return this.ctx.response.error(error.message);
    }
  }
}

module.exports = AdminService;
